<template>
  <div class="withdrawal-container">
    <el-card class="withdrawal-card">
      <template #header>
        <div class="card-header">
          <span>提现管理</span>
          <div class="header-operations">
            <el-input
              v-model="searchQuery"
              placeholder="搜索用户名/订单号"
              style="width: 200px; margin-right: 10px;"
            />
            <el-button type="primary" @click="handleSearch">搜索</el-button>
          </div>
        </div>
      </template>
      
      <el-table :data="withdrawalList" style="width: 100%">
        <el-table-column prop="id" label="订单号" width="180" />
        <el-table-column prop="userName" label="用户名" />
        <el-table-column prop="amount" label="提现金额" width="120">
          <template #default="scope">
            ¥{{ scope.row.amount }}
          </template>
        </el-table-column>
        <el-table-column prop="bankName" label="银行名称" width="120" />
        <el-table-column prop="bankAccount" label="银行账号" width="180" />
        <el-table-column prop="accountName" label="开户人" width="120" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="申请时间" width="180" />
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button 
              type="success" 
              size="small" 
              @click="handleApprove(scope.row)"
              v-if="scope.row.status === '待审核'"
            >通过</el-button>
            <el-button 
              type="danger" 
              size="small" 
              @click="handleReject(scope.row)"
              v-if="scope.row.status === '待审核'"
            >拒绝</el-button>
            <el-button type="primary" size="small" @click="handleDetail(scope.row)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 提现详情对话框 -->
    <el-dialog
      v-model="dialogVisible"
      title="提现详情"
      width="600px"
    >
      <el-descriptions :column="2" border>
        <el-descriptions-item label="订单号">{{ currentWithdrawal.id }}</el-descriptions-item>
        <el-descriptions-item label="用户名">{{ currentWithdrawal.userName }}</el-descriptions-item>
        <el-descriptions-item label="提现金额">¥{{ currentWithdrawal.amount }}</el-descriptions-item>
        <el-descriptions-item label="银行名称">{{ currentWithdrawal.bankName }}</el-descriptions-item>
        <el-descriptions-item label="银行账号">{{ currentWithdrawal.bankAccount }}</el-descriptions-item>
        <el-descriptions-item label="开户人">{{ currentWithdrawal.accountName }}</el-descriptions-item>
        <el-descriptions-item label="状态">
          <el-tag :type="getStatusType(currentWithdrawal.status)">
            {{ currentWithdrawal.status }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="申请时间">{{ currentWithdrawal.createTime }}</el-descriptions-item>
        <el-descriptions-item label="处理时间" v-if="currentWithdrawal.processTime">
          {{ currentWithdrawal.processTime }}
        </el-descriptions-item>
        <el-descriptions-item label="处理说明" v-if="currentWithdrawal.remark" :span="2">
          {{ currentWithdrawal.remark }}
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>

    <!-- 拒绝原因对话框 -->
    <el-dialog
      v-model="rejectDialogVisible"
      title="拒绝提现"
      width="500px"
    >
      <el-form :model="rejectForm" :rules="rejectRules" ref="rejectFormRef" label-width="80px">
        <el-form-item label="拒绝原因" prop="reason">
          <el-input
            v-model="rejectForm.reason"
            type="textarea"
            :rows="3"
            placeholder="请输入拒绝原因"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="rejectDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleRejectSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 搜索相关
const searchQuery = ref('')

// 分页相关
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

// 提现列表数据
const withdrawalList = ref([
  {
    id: 'WD202404050001',
    userName: '用户A',
    amount: 1000.00,
    bankName: '中国银行',
    bankAccount: '6222************1234',
    accountName: '张三',
    status: '待审核',
    createTime: '2024-04-05 10:00:00'
  },
  {
    id: 'WD202404050002',
    userName: '用户B',
    amount: 2000.00,
    bankName: '工商银行',
    bankAccount: '6222************5678',
    accountName: '李四',
    status: '已通过',
    createTime: '2024-04-05 11:00:00',
    processTime: '2024-04-05 11:30:00',
    remark: '审核通过'
  }
])

// 对话框相关
const dialogVisible = ref(false)
const rejectDialogVisible = ref(false)
const currentWithdrawal = ref({})
const rejectFormRef = ref(null)

// 拒绝表单数据
const rejectForm = reactive({
  reason: ''
})

// 拒绝表单验证规则
const rejectRules = {
  reason: [
    { required: true, message: '请输入拒绝原因', trigger: 'blur' },
    { min: 5, max: 200, message: '长度在 5 到 200 个字符', trigger: 'blur' }
  ]
}

// 获取状态对应的标签类型
const getStatusType = (status) => {
  const statusMap = {
    '待审核': 'warning',
    '已通过': 'success',
    '已拒绝': 'danger',
    '已打款': 'success'
  }
  return statusMap[status] || 'info'
}

// 搜索
const handleSearch = () => {
  // TODO: 调用搜索接口
  console.log('搜索关键词：', searchQuery.value)
}

// 查看详情
const handleDetail = (row) => {
  currentWithdrawal.value = row
  dialogVisible.value = true
}

// 通过提现
const handleApprove = (row) => {
  ElMessageBox.confirm(
    '确定要通过该提现申请吗？',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    // TODO: 调用通过接口
    ElMessage.success('审核通过')
  }).catch(() => {})
}

// 拒绝提现
const handleReject = (row) => {
  currentWithdrawal.value = row
  rejectForm.reason = ''
  rejectDialogVisible.value = true
}

// 提交拒绝
const handleRejectSubmit = () => {
  rejectFormRef.value.validate((valid) => {
    if (valid) {
      // TODO: 调用拒绝接口
      ElMessage.success('已拒绝提现申请')
      rejectDialogVisible.value = false
    }
  })
}

// 分页大小改变
const handleSizeChange = (val) => {
  pageSize.value = val
  // TODO: 重新加载数据
}

// 当前页改变
const handleCurrentChange = (val) => {
  currentPage.value = val
  // TODO: 重新加载数据
}
</script>

<style scoped lang="scss">
.withdrawal-container {
  padding: 20px;
}

.withdrawal-card {
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .header-operations {
    display: flex;
    align-items: center;
  }
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style> 